<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */

    /**
     * Manage the custom configuration of the project.
     *
     * @package    kassiop
     * @subpackage managers
     *
     * @author nairus
     *
     */
    class ConfigManager
    {
        /**
         * Creates an instance of ConfigManager
         *
         * @param  string    $file        The path of the custom configuration.
         * @param  boolean   $withSection Defines if the config file must be parsed with sections.
         * @throws Exception              If the config file cannot be parsed.
         */
        public function __construct( $file = null, $withSection = true )
        {
            $this->withSections = $withSection;

            // Tests if the custom file does not exist
            if( $file == null || !is_file( $file ) )
            {
                Configuration::includeBootstrap();

                // Tests if the custom config file exists.
                if( file_exists( CONFIG_MAIN_PATH . self::CONFIG_FILE_NAME ) )
                {
                    $file = CONFIG_MAIN_PATH . DS . self::CONFIG_FILE_NAME ;
                }
                else
                {
                    if( file_exists( CONFIG_MAIN_PATH . self::DEFAULT_CONFIG_FILE_NAME ) )
                    {
                        $file = CONFIG_MAIN_PATH . DS . self::DEFAULT_CONFIG_FILE_NAME ;
                    }
                }
            }

            // try to parse the config file.
            $this->config = parse_ini_file( $file, $this->withSections );
            if( $this->config == false )
            {
                throw new Exception('The config file "' . $file .'" cannot be parsed!' );
            }

        }

        /**
         * The default name of config file.
         */
        const DEFAULT_CONFIG_FILE_NAME = "config.ini.default" ;

        /**
         * The default path of mysql.properties.
         */
        const CONFIG_FILE_NAME = "config.ini" ;

        /**
         * The configuration file parsed in array.
         *
         * @var array
         */
        protected $config = array();

        /**
         * Defines if the config file has been parsed with section.
         *
         * @var boolean
         */
        protected $withSection;

        /**
         * Returns the config property.
         *
         * @param  string $property
         * @param  string $section
         * @return mixed  The config file can return the all scalar types and only array compound type.
         *
         * @see http://www.phpbuilder.com/manual/language.types.php for more explainations.
         */
        public function get( $property, $section = null )
        {
            if( $section != null )
            {
                return isset( $this->config[$section][$property] ) ? $this->config[$section][$property]  : '' ;
            }
            else
            {
                return isset( $this->config[$property] ) ? $this->config[$property]  : '' ;
            }
        }

        /**
         * Return the path of the frontend config.
         *
         * @return string
         */
        public function getFrontConfigPath()
        {
            Configuration::includeBootstrap() ;
            return LIBS_PATH . $this->get( APPLICATIONS_LIBS_PROPERTY, APPLICATIONS_SECTION ) . DS . $this->get( NAME_PROPERTY, FRONTEND_SECTION ) . DS . CONFIG_FOLDER . DS;
        }

        /**
         * Return the path of the backend config.
         *
         * @return string
         */
        public function getBackConfigPath()
        {
            Configuration::includeBootstrap() ;
            return LIBS_PATH . $this->get( APPLICATIONS_LIBS_PROPERTY, APPLICATIONS_SECTION ) . DS . $this->get( NAME_PROPERTY, BACKEND_SECTION ) . DS . CONFIG_FOLDER . DS;
        }
    }
?>